package org.rambadger.deployer.resource;

import org.rambadger.deployer.response.CommandResponse;
import org.rambadger.deployer.service.CommandServerService;
import org.rambadger.deployer.utils.DeployerConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.HttpURLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: rtompkins
 * Date: 10/18/13
 * Time: 9:45 AM
 * To change this template use File | Settings | File Templates.
 */
@Path("/command-server")
@Component
@Scope("request")
public class CommandServerResource {

    @Autowired
    private CommandServerService service;

    private static final String CLASSNAME = CommandServerResource.class.getName();
    private static final Logger LOGGER = Logger.getLogger(CLASSNAME);

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/stop")
    public Response stopCommandApp(@QueryParam("auth") String auth) {
        final String methodName = "stopCommandApp";
        LOGGER.logp(Level.INFO, CLASSNAME, methodName, "Resource Entered");

        CommandResponse response = new CommandResponse();

        if (auth.equals(DeployerConstants.PASSWORD)) {
            if (service.stopCommandApp().equals(Boolean.TRUE)) {
                response.setInfo("RAMBADGER: command server successfully stopped");
                response.setHttpResponseCode(HttpURLConnection.HTTP_OK);
            } else {
                response.setInfo("RAMBADGER: command failed to stop");
                response.setHttpResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR);
            }
        } else {
            response.setHttpResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED);
        }

        return Response.status(response.getHttpResponseCode().getValue()).entity(response).build();

    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/start")
    public Response startCommandApp(@QueryParam("auth") String auth) {
        final String methodName = "startCommandApp";
        LOGGER.logp(Level.INFO, CLASSNAME, methodName, "Resource Entered");

        CommandResponse response = new CommandResponse();

        if (auth.equals(DeployerConstants.PASSWORD)) {
            if (service.startCommandApp().equals(Boolean.TRUE)) {
                response.setInfo("RAMBADGER: command successfully started");
                response.setHttpResponseCode(HttpURLConnection.HTTP_OK);
            } else {
                response.setInfo("RAMBADGER: command failed to start");
                response.setHttpResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR);
            }
        } else {
            response.setHttpResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED);
        }

        return Response.status(response.getHttpResponseCode().getValue()).entity(response).build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/deploy-newest")
    public Response deployNewestCommandApp(@QueryParam("auth") String auth) {
        final String methodName = "deployNewestCommandApp";
        LOGGER.logp(Level.INFO, CLASSNAME, methodName, "Resource Entered");

        CommandResponse response = new CommandResponse();

        if (auth.equals(DeployerConstants.PASSWORD)) {
            if (service.deployCommandAppFromNexus().equals(Boolean.TRUE)) {
                response.setInfo("RAMBADGER: newest command fat-jar successfully deployed");
                response.setHttpResponseCode(HttpURLConnection.HTTP_OK);
            } else {
                response.setInfo("RAMBADGER: newest command fat-jar failed to deploy successfully");
                response.setHttpResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR);
            }
        } else {
            response.setHttpResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED);
        }

        return Response.status(response.getHttpResponseCode().getValue()).entity(response).build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/restart")
    public Response restartCommandApp(@QueryParam("auth") String auth) {
        final String methodName = "restartCommandApp";
        LOGGER.logp(Level.INFO, CLASSNAME, methodName, "Resource Entered");

        CommandResponse response = new CommandResponse();

        if (auth.equals(DeployerConstants.PASSWORD)) {
            if (service.restartCommandApp().equals(Boolean.TRUE)) {
                response.setInfo("RAMBADGER: command server successfully restarted");
                response.setHttpResponseCode(HttpURLConnection.HTTP_OK);
            } else {
                response.setInfo("RAMBADGER: command server failed to restart");
                response.setHttpResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR);
            }
        } else {
            response.setHttpResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED);
        }

        return Response.status(response.getHttpResponseCode().getValue()).entity(response).build();
    }

}
